package net.hn.hnms.biz.meianshen.service;

import KHThrift.*;
import net.hn.hnms.biz.meianshen.domain.*;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 智呈物联 pan'sx
 * 2023/3/3 11:02
 * 1.0 version
 */
@Service
public class KHDataService {
    private static KDBApi m_Client = new KDBApi();

    public void TestKDBServerConnect() {
        System.out.println("======TestKDBServerConnect======");

        ThriftConnectOption ConnOption = new ThriftConnectOption();
        ConnOption.ServerName = "192.168.52.12";
        ConnOption.ServerPort = 6789;
        ConnOption.UserName = "sa";
        ConnOption.Password = "sa";
        ConnOption.NetworkTimeout = 0;

        ThriftConnectRet Ret = null;
        try {
            Ret = m_Client.KDBServerConnect(ConnOption);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {
                System.out.println("Connect succeed");
            } else {
                System.out.println(String.format("Connect failed:%d", Ret.Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    public void TestKDBServerDisconnect() {
        System.out.println("======TestKDBServerDisconnect======");
        int Ret = 0;
        try {
            Ret = m_Client.KDBServerDisconnect();
            if (Ret == khbrokerConstants.THRIFT_KERR_OK) {
                System.out.println("DisConnect succeed");
            } else {
                System.out.println(String.format("Disconnect failed:%d", Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    public void TestKDBTagOpenRecordset() {
        System.out.println("======TestKDBTagOpenRecordset======");

        ThriftTagCriteria TagCriteria = new ThriftTagCriteria();

        TagCriteria.TagNames = new ArrayList<String>();
        TagCriteria.TagNames.add("Tag000000");
        TagCriteria.TagNames.add("Tag000001");

        ThriftTagFields TagFields = new ThriftTagFields();
        TagFields.AllFields = true;

        try {
            ThriftTagOpenRecordsetRet Ret = m_Client.KDBTagOpenRecordset(TagCriteria, TagFields);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {
                for (int i = 0; i < Ret.TagRecordset.size(); ++i) {
                    System.out.println(String.format("MaxValue:%f", Ret.TagRecordset.get(i).MaxValue));
                }
            } else {
                System.out.println(String.format("Open recordset failed:%d", Ret.Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    public void TestKDBTagGetAllNames() {
        System.out.println("======TestKDBTagGetAllNames======");

        try {
            ThriftTagGetNamesRet Ret = m_Client.KDBTagGetAllNames();
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {
                for (int i = 0; i < Ret.TagNames.size(); ++i) {
                    System.out.println(Ret.TagNames.get(i));
                }
            } else {
                System.out.println(String.format("Get tag all names failed:%d", Ret.Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    public void TestKDBTagGroupGetTags(int grouId) {
        System.out.println("======TestKDBTagGroupGetTags======");

        try {
            ThriftTagGroupGetTagsRet Ret = m_Client.KDBTagGroupGetTags(grouId);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {
                for (int i = 0; i < Ret.TagNames.size(); ++i) {
                    System.out.println(Ret.TagNames.get(i));
                }
            } else {
                System.out.println(String.format("Tag group get tags failed:%d", Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    public void test() throws ClassNotFoundException, SQLException {
        String driver = "com.kh.jdbc.Driver";
        String url = "jdbc:kh://192.168.52.12:5678";
        ;
        String user = "sa";
        String pwd = "sa";

        Class.forName(driver);
        Connection con = DriverManager.getConnection(url, user, pwd);
        System.out.println(con.getClientInfo());
    }

    public synchronized List<Belt> TestKDBDataGetCurrentValue(List<String> TagNames) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        System.out.println("皮带数据执行");
        List<Belt> list = new ArrayList<>();
        try {
            ThriftDataGetCurrentValueRet Ret = m_Client.KDBDataGetCurrentValue(TagNames);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {

                List<KDBDataProperties> r = KDBConvert.Convert(Ret.DataProperties);

                for (int n = 0; n < r.size(); ++n) {
                    Belt baseEntity = new Belt();
                    baseEntity.setTagName(TagNames.get(n));
//                    System.out.println(r.get(n).TimeStamp.Seconds);
                    baseEntity.setDateTime(new Date(Long.parseLong(r.get(n).TimeStamp.Seconds + "") * 1000));
                    baseEntity.setQuality(r.get(n).Quality == khbrokerConstants.KDAQ_OPC_GOOD);
                    switch (r.get(n).Value.DataType) {
                        case khbrokerConstants.THRIFT_KVDT_BOOL:
                            baseEntity.setVal(r.get(n).Value.Value.bitVal+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I1:
                            baseEntity.setVal(r.get(n).Value.Value.i1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI1:
                            baseEntity.setVal(r.get(n).Value.Value.ui1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I2:
                            baseEntity.setVal(r.get(n).Value.Value.i2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI2:
                            baseEntity.setVal(r.get(n).Value.Value.ui2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I4:
                            baseEntity.setVal(r.get(n).Value.Value.i4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI4:
                            baseEntity.setVal(r.get(n).Value.Value.ui4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I8:
                            baseEntity.setVal(r.get(n).Value.Value.i8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI8:
                            baseEntity.setVal(r.get(n).Value.Value.ui8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R4:
                            baseEntity.setVal(r.get(n).Value.Value.r4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R8:
                            baseEntity.setVal(r.get(n).Value.Value.r8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_STR:
                            baseEntity.setVal(r.get(n).Value.Value.strVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_WSTR:
                            baseEntity.setVal(r.get(n).Value.Value.wstrVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_TIMESTAMP:
                            baseEntity.setVal(r.get(n).Value.Value.tsVal.Seconds+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_FILETIME:
                            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date(r.get(n).Value.Value.ftVal));
                            baseEntity.setVal(date);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_EMPTY:
                            baseEntity.setVal(r.get(n).Value.Value.emptyVal+"");
                            break;
                        default:
                            System.err.println("未匹配到类型"+r.get(n).Value.DataType);
                            break;
                    }
                    list.add(baseEntity);
                }

            } else {
                System.out.println(String.format("Data get current value failed:%d", Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<String> TestKDBTagGetNames(String tagName) {
        System.out.println("======TestKDBTagGetNames======");

        try {
            ThriftTagGetNamesRet Ret = m_Client.KDBTagGetNames(tagName, null, null, null);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {
                return Ret.TagNames;
            } else {
                System.out.println(String.format("Tag get names failed:%d", Ret.Ret));
            }

        } catch (TException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

//    public void TestKDBTagGetProperties(String tagName) {
//        System.out.println("======TestKDBTagGetProperties======");
//
//        ThriftTagFields TagFields = new ThriftTagFields();
//        TagFields.AllFields = true;
//
//        try {
//            ThriftTagGetPropertiesRet Ret = m_Client.KDBTagGetProperties(tagName, TagFields);
//            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {
//                System.out.println(String.format("Description:%f", Ret.TagProperties.Description));
//            } else {
//                System.out.println(String.format("Tag get properties failed:%d", Ret.Ret));
//            }
//        } catch (TException e) {
//            e.printStackTrace();
//        }
//    }


    public synchronized List<DrainWater> drainWaterGetCurrentValue(List<String> TagNames) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        System.out.println("水泵数据执行");
        List<DrainWater> list = new ArrayList<>();
        try {
            ThriftDataGetCurrentValueRet Ret = m_Client.KDBDataGetCurrentValue(TagNames);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {

                List<KDBDataProperties> r = KDBConvert.Convert(Ret.DataProperties);

                for (int n = 0; n < r.size(); ++n) {
                    DrainWater baseEntity = new DrainWater();
                    baseEntity.setTagName(TagNames.get(n));
                    baseEntity.setDateTime(new Date(Long.parseLong(r.get(n).TimeStamp.Seconds + "") * 1000));
                    baseEntity.setQuality(r.get(n).Quality == khbrokerConstants.KDAQ_OPC_GOOD);
                    switch (r.get(n).Value.DataType) {
                        case khbrokerConstants.THRIFT_KVDT_BOOL:
                            baseEntity.setVal(r.get(n).Value.Value.bitVal+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I1:
                            baseEntity.setVal(r.get(n).Value.Value.i1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI1:
                            baseEntity.setVal(r.get(n).Value.Value.ui1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I2:
                            baseEntity.setVal(r.get(n).Value.Value.i2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI2:
                            baseEntity.setVal(r.get(n).Value.Value.ui2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I4:
                            baseEntity.setVal(r.get(n).Value.Value.i4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI4:
                            baseEntity.setVal(r.get(n).Value.Value.ui4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I8:
                            baseEntity.setVal(r.get(n).Value.Value.i8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI8:
                            baseEntity.setVal(r.get(n).Value.Value.ui8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R4:
                            baseEntity.setVal(r.get(n).Value.Value.r4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R8:
                            baseEntity.setVal(r.get(n).Value.Value.r8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_STR:
                            baseEntity.setVal(r.get(n).Value.Value.strVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_WSTR:
                            baseEntity.setVal(r.get(n).Value.Value.wstrVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_TIMESTAMP:
                            baseEntity.setVal(r.get(n).Value.Value.tsVal.Seconds+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_FILETIME:
                            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date(r.get(n).Value.Value.ftVal));
                            baseEntity.setVal(date);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_EMPTY:
                            baseEntity.setVal(r.get(n).Value.Value.emptyVal+"");
                            break;
                        default:
                            System.err.println("未匹配到类型"+r.get(n).Value.DataType);
                            break;
                    }
                    list.add(baseEntity);
                }

            } else {
                System.out.println(String.format("Data get current value failed:%d", Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return list;
    }

    public synchronized List<CompressedNitrogen> compressedNitrogenGetCurrentValue(List<String> TagNames)  {
        System.out.println("压风制氮数据执行");
        List<CompressedNitrogen> list = new ArrayList<>();
        try {
            ThriftDataGetCurrentValueRet Ret = m_Client.KDBDataGetCurrentValue(TagNames);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {

                List<KDBDataProperties> r = KDBConvert.Convert(Ret.DataProperties);

                for (int n = 0; n < r.size(); ++n) {
                    CompressedNitrogen baseEntity = new CompressedNitrogen();
                    baseEntity.setTagName(TagNames.get(n));
                    baseEntity.setDateTime(new Date(Long.parseLong(r.get(n).TimeStamp.Seconds + "") * 1000));
                    baseEntity.setQuality(r.get(n).Quality == khbrokerConstants.KDAQ_OPC_GOOD);
                    switch (r.get(n).Value.DataType) {
                        case khbrokerConstants.THRIFT_KVDT_BOOL:
                            baseEntity.setVal(r.get(n).Value.Value.bitVal+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I1:
                            baseEntity.setVal(r.get(n).Value.Value.i1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI1:
                            baseEntity.setVal(r.get(n).Value.Value.ui1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I2:
                            baseEntity.setVal(r.get(n).Value.Value.i2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI2:
                            baseEntity.setVal(r.get(n).Value.Value.ui2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I4:
                            baseEntity.setVal(r.get(n).Value.Value.i4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI4:
                            baseEntity.setVal(r.get(n).Value.Value.ui4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I8:
                            baseEntity.setVal(r.get(n).Value.Value.i8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI8:
                            baseEntity.setVal(r.get(n).Value.Value.ui8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R4:
                            baseEntity.setVal(r.get(n).Value.Value.r4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R8:
                            baseEntity.setVal(r.get(n).Value.Value.r8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_STR:
                            baseEntity.setVal(r.get(n).Value.Value.strVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_WSTR:
                            baseEntity.setVal(r.get(n).Value.Value.wstrVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_TIMESTAMP:
                            baseEntity.setVal(r.get(n).Value.Value.tsVal.Seconds+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_FILETIME:
                            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date(r.get(n).Value.Value.ftVal));
                            baseEntity.setVal(date);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_EMPTY:
                            baseEntity.setVal(r.get(n).Value.Value.emptyVal+"");
                            break;
                        default:
                            System.err.println("未匹配到类型"+r.get(n).Value.DataType);
                            break;
                    }
                    list.add(baseEntity);
                }

            } else {
                System.out.println(String.format("Data get current value failed:%d", Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return list;
    }
    public synchronized List<Mining> miningGetCurrentValue(List<String> TagNames)  {
        System.out.println("采煤机数据执行");
        List<Mining> list = new ArrayList<>();
        try {
            ThriftDataGetCurrentValueRet Ret = m_Client.KDBDataGetCurrentValue(TagNames);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {

                List<KDBDataProperties> r = KDBConvert.Convert(Ret.DataProperties);

                for (int n = 0; n < r.size(); ++n) {
                    Mining baseEntity = new Mining();
                    baseEntity.setTagName(TagNames.get(n));
                    baseEntity.setDateTime(new Date(Long.parseLong(r.get(n).TimeStamp.Seconds + "") * 1000));
                    baseEntity.setQuality(r.get(n).Quality == khbrokerConstants.KDAQ_OPC_GOOD);
                    switch (r.get(n).Value.DataType) {
                        case khbrokerConstants.THRIFT_KVDT_BOOL:
                            baseEntity.setVal(r.get(n).Value.Value.bitVal+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I1:
                            baseEntity.setVal(r.get(n).Value.Value.i1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI1:
                            baseEntity.setVal(r.get(n).Value.Value.ui1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I2:
                            baseEntity.setVal(r.get(n).Value.Value.i2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI2:
                            baseEntity.setVal(r.get(n).Value.Value.ui2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I4:
                            baseEntity.setVal(r.get(n).Value.Value.i4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI4:
                            baseEntity.setVal(r.get(n).Value.Value.ui4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I8:
                            baseEntity.setVal(r.get(n).Value.Value.i8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI8:
                            baseEntity.setVal(r.get(n).Value.Value.ui8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R4:
                            baseEntity.setVal(r.get(n).Value.Value.r4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R8:
                            baseEntity.setVal(r.get(n).Value.Value.r8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_STR:
                            baseEntity.setVal(r.get(n).Value.Value.strVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_WSTR:
                            baseEntity.setVal(r.get(n).Value.Value.wstrVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_TIMESTAMP:
                            baseEntity.setVal(r.get(n).Value.Value.tsVal.Seconds+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_FILETIME:
                            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date(r.get(n).Value.Value.ftVal));
                            baseEntity.setVal(date);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_EMPTY:
                            baseEntity.setVal(r.get(n).Value.Value.emptyVal+"");
                            break;
                        default:
                            System.err.println("未匹配到类型"+r.get(n).Value.DataType);
                            break;
                    }
                    list.add(baseEntity);
                }

            } else {
                System.out.println(String.format("Data get current value failed:%d", Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return list;
    }

    public synchronized List<Power> powerGetCurrentValue(List<String> TagNames)  {
        System.out.println("供电数据执行");
        List<Power> list = new ArrayList<>();
        try {
            ThriftDataGetCurrentValueRet Ret = m_Client.KDBDataGetCurrentValue(TagNames);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {

                List<KDBDataProperties> r = KDBConvert.Convert(Ret.DataProperties);

                for (int n = 0; n < r.size(); ++n) {
                    Power baseEntity = new Power();
                    baseEntity.setTagName(TagNames.get(n));
                    baseEntity.setDateTime(new Date(Long.parseLong(r.get(n).TimeStamp.Seconds + "") * 1000));
                    baseEntity.setQuality(r.get(n).Quality == khbrokerConstants.KDAQ_OPC_GOOD);
                    switch (r.get(n).Value.DataType) {
                        case khbrokerConstants.THRIFT_KVDT_BOOL:
                            baseEntity.setVal(r.get(n).Value.Value.bitVal+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I1:
                            baseEntity.setVal(r.get(n).Value.Value.i1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI1:
                            baseEntity.setVal(r.get(n).Value.Value.ui1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I2:
                            baseEntity.setVal(r.get(n).Value.Value.i2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI2:
                            baseEntity.setVal(r.get(n).Value.Value.ui2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I4:
                            baseEntity.setVal(r.get(n).Value.Value.i4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI4:
                            baseEntity.setVal(r.get(n).Value.Value.ui4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I8:
                            baseEntity.setVal(r.get(n).Value.Value.i8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI8:
                            baseEntity.setVal(r.get(n).Value.Value.ui8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R4:
                            baseEntity.setVal(r.get(n).Value.Value.r4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R8:
                            baseEntity.setVal(r.get(n).Value.Value.r8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_STR:
                            baseEntity.setVal(r.get(n).Value.Value.strVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_WSTR:
                            baseEntity.setVal(r.get(n).Value.Value.wstrVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_TIMESTAMP:
                            baseEntity.setVal(r.get(n).Value.Value.tsVal.Seconds+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_FILETIME:
                            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date(r.get(n).Value.Value.ftVal));
                            baseEntity.setVal(date);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_EMPTY:
                            baseEntity.setVal(r.get(n).Value.Value.emptyVal+"");
                            break;
                        default:
                            System.err.println("未匹配到类型"+r.get(n).Value.DataType);
                            break;
                    }
                    list.add(baseEntity);
                }

            } else {
                System.out.println(String.format("Data get current value failed:%d", Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return list;
    }
    public synchronized List<Fan> fanGetCurrentValue(List<String> TagNames)  {
        System.out.println("通风数据执行");
        List<Fan> list = new ArrayList<>();
        try {
            ThriftDataGetCurrentValueRet Ret = m_Client.KDBDataGetCurrentValue(TagNames);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {

                List<KDBDataProperties> r = KDBConvert.Convert(Ret.DataProperties);

                for (int n = 0; n < r.size(); ++n) {
                    Fan baseEntity = new Fan();
                    baseEntity.setTagName(TagNames.get(n));
                    baseEntity.setDateTime(new Date(Long.parseLong(r.get(n).TimeStamp.Seconds + "") * 1000));
                    baseEntity.setQuality(r.get(n).Quality == khbrokerConstants.KDAQ_OPC_GOOD);
                    switch (r.get(n).Value.DataType) {
                        case khbrokerConstants.THRIFT_KVDT_BOOL:
                            baseEntity.setVal(r.get(n).Value.Value.bitVal+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I1:
                            baseEntity.setVal(r.get(n).Value.Value.i1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI1:
                            baseEntity.setVal(r.get(n).Value.Value.ui1Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I2:
                            baseEntity.setVal(r.get(n).Value.Value.i2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI2:
                            baseEntity.setVal(r.get(n).Value.Value.ui2Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I4:
                            baseEntity.setVal(r.get(n).Value.Value.i4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI4:
                            baseEntity.setVal(r.get(n).Value.Value.ui4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_I8:
                            baseEntity.setVal(r.get(n).Value.Value.i8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_UI8:
                            baseEntity.setVal(r.get(n).Value.Value.ui8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R4:
                            baseEntity.setVal(r.get(n).Value.Value.r4Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_R8:
                            baseEntity.setVal(r.get(n).Value.Value.r8Val+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_STR:
                            baseEntity.setVal(r.get(n).Value.Value.strVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_WSTR:
                            baseEntity.setVal(r.get(n).Value.Value.wstrVal);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_TIMESTAMP:
                            baseEntity.setVal(r.get(n).Value.Value.tsVal.Seconds+"");
                            break;
                        case khbrokerConstants.THRIFT_KVDT_FILETIME:
                            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date(r.get(n).Value.Value.ftVal));
                            baseEntity.setVal(date);
                            break;
                        case khbrokerConstants.THRIFT_KVDT_EMPTY:
                            baseEntity.setVal(r.get(n).Value.Value.emptyVal+"");
                            break;
                        default:
                            System.err.println("未匹配到类型"+r.get(n).Value.DataType);
                            break;
                    }
                    list.add(baseEntity);
                }

            } else {
                System.out.println(String.format("Data get current value failed:%d", Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<PointDescription> TestKDBTagGetNames() {
        System.out.println("======TestKDBTagGetNames======");
        List<PointDescription> list = new ArrayList<>();
        try {
            ThriftTagGetNamesRet Ret = m_Client.KDBTagGetNames("ST*", null, null, null);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {
                for (int i = 0; i < Ret.TagNames.size(); ++i) {
                    PointDescription pd = new PointDescription();
                    String s = Ret.TagNames.get(i);
                    pd.setCode(s);
                    pd.setName(TestKDBTagGetProperties(s));
                    list.add(pd);
                }
            } else {
                System.out.println(String.format("Tag get names failed:%d", Ret.Ret));
            }

        } catch (TException e) {
            e.printStackTrace();
        }
        return list;
    }

    public String TestKDBTagGetProperties(String tagNmae) {
        System.out.println("======TestKDBTagGetProperties======");

        ThriftTagFields TagFields = new ThriftTagFields();
        TagFields.AllFields = true;

        try {
            ThriftTagGetPropertiesRet Ret = m_Client.KDBTagGetProperties(tagNmae, TagFields);
            if (Ret.Ret == khbrokerConstants.THRIFT_KERR_OK) {
//                System.out.println(String.format("MaxValue:%f", Ret.TagProperties.Description));
                return Ret.TagProperties.Description;
            } else {
                System.out.println(String.format("Tag get properties failed:%d", Ret.Ret));
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return "";
    }
}
